﻿#region Header
// Copyright (c) 2015 OPEN TONE, Inc.
// Released under the MIT license
// http://opensource.org/licenses/mit-license.php
#endregion
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenXmlLib.Excel;

namespace OpenXmlLibTest.Excel.Impl
{
	[TestClass()]
	public class CellImplTest : AbstractExcelTestBase
	{
		/// <summary>
		/// Cellの取得
		/// </summary>
		[TestMethod()]
		public void TestIsEmpty01()
		{
			IExcel excel = Open("CellImplTest.TestIsEmpty01.xlsx");
			Assert.IsNotNull(excel);

			IWorksheet worksheet = excel.Workbook.GetWorksheet("001");
			Assert.IsNotNull(worksheet);

			// 値が設定されているセル(大文字指定)
			ICell cell = worksheet.GetCell("B6");
			Assert.IsFalse(cell.IsEmpty());
			// 値が設定されているセル(小文字指定)
			cell = worksheet.GetCell("b6");
			Assert.IsFalse(cell.IsEmpty());

			// 値が設定されていないセル
			cell = worksheet.GetCell("Z1");
			Assert.IsTrue(cell.IsEmpty());
			// 存在しないセル(小文字指定)
			cell = worksheet.GetCell("AABB123");
			Assert.IsTrue(cell.IsEmpty());
		}

		/// <summary>
		/// Cell値の取得
		/// </summary>
		[TestMethod()]
		public void TestGetStringValue01()
		{
			IExcel excel = Open("CellImplTest.TestGetStringValue01.xlsx");
			Assert.IsNotNull(excel);

			IWorksheet worksheet = excel.Workbook.GetWorksheet("001");
			Assert.IsNotNull(worksheet);

			// 書式：標準
			ICell cell = worksheet.GetCell("C3");
			Assert.AreEqual("123", cell.GetStringValue());
			// 書式：数値
			cell = worksheet.GetCell("C4");
			double d = Convert.ToDouble(cell.GetStringValue());
			Assert.AreEqual(466.778, d);

			// 書式：パーセント(画面上は20%)
			cell = worksheet.GetCell("C5");
			d = Convert.ToDouble(cell.GetStringValue());
			Assert.AreEqual(0.2, d);

			// 書式：日付
			cell = worksheet.GetCell("C6");
			long l = Convert.ToInt64(cell.GetStringValue());
			Assert.AreEqual(42369, l);

			// 書式：文字列
			cell = worksheet.GetCell("C7");
			Assert.AreEqual("2001/12/31", cell.GetStringValue());

			// 数式(真偽TRUE)
			cell = worksheet.GetCell("C8");
			Assert.AreEqual("TRUE", cell.GetStringValue());

			// 数式(真偽FALSE)
			cell = worksheet.GetCell("C9");
			Assert.AreEqual("FALSE", cell.GetStringValue());

			// 数式(数値型)
			cell = worksheet.GetCell("C10");
			d = Convert.ToDouble(cell.GetStringValue());
			Assert.AreEqual(24.6, d);

			// 数式(文字列型)
			cell = worksheet.GetCell("C11");
			Assert.AreEqual("2015年12月31日", cell.GetStringValue());

			// 値無し
			cell = worksheet.GetCell("C12");
			Assert.AreEqual("", cell.GetStringValue());
		}

		/// <summary>
		/// Cell値の取得
		/// </summary>
		[TestMethod()]
		public void TestGetBooleanValue01()
		{
			IExcel excel = Open("CellImplTest.TestGetBooleanValue01.xlsx");
			Assert.IsNotNull(excel);

			IWorksheet worksheet = excel.Workbook.GetWorksheet("001");
			Assert.IsNotNull(worksheet);

			// 書式：標準
			ICell cell = worksheet.GetCell("C3");
			Assert.IsNull(cell.GetBooleanValue());

			// 書式：数値
			cell = worksheet.GetCell("C4");
			Assert.IsNull(cell.GetBooleanValue());

			// 書式：パーセント(画面上は20%)
			cell = worksheet.GetCell("C5");
			Assert.IsNull(cell.GetBooleanValue());

			// 書式：日付
			cell = worksheet.GetCell("C6");
			Assert.IsNull(cell.GetBooleanValue());

			// 書式：文字列
			cell = worksheet.GetCell("C7");
			Assert.IsNull(cell.GetBooleanValue());

			// 数式(真偽TRUE)
			cell = worksheet.GetCell("C8");
			Assert.AreEqual(true, cell.GetBooleanValue());

			// 数式(真偽FALSE)
			cell = worksheet.GetCell("C9");
			Assert.AreEqual(false, cell.GetBooleanValue());

			// 数式(数値型)
			cell = worksheet.GetCell("C10");
			Assert.IsNull(cell.GetBooleanValue());

			// 数式(文字列型)
			cell = worksheet.GetCell("C11");
			Assert.IsNull(cell.GetBooleanValue());

			// 値無し
			cell = worksheet.GetCell("C12");
			Assert.IsNull(cell.GetBooleanValue());
		}
	}
}
